package com.cytech.cloudclinic.moudle.main.patient.add;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.base.BaseActivity;
import com.cytech.cloudclinic.bean.Patient;
import com.cytech.cloudclinic.utils.TimeUtils;
import com.sly.pylibrary.Cn2Py;
import com.yechaoa.yutils.ToastUtil;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加患者
 */
public class AddPatientActivity extends BaseActivity<AddPatientPresenter> implements AddPatientView {

    @BindView(R.id.iv_add_patient_back)
    ImageView ivAddPatientBack;
    @BindView(R.id.et_add_patient_name)
    EditText etAddPatientName;
    @BindView(R.id.et_add_patient_name_py)
    TextView etAddPatientNamePy;
    @BindView(R.id.tv_add_patient_birthday)
    TextView tvAddPatientBirthday;
    @BindView(R.id.et_add_patient_age)
    TextView etAddPatientAge;
    @BindView(R.id.et_add_patient_phone)
    EditText etAddPatientPhone;
    @BindView(R.id.et_add_patient_address)
    EditText etAddPatientAddress;
    @BindView(R.id.btn_add_patient)
    TextView btnAddPatient;
    @BindView(R.id.rb_add_patient_man)
    RadioButton rbAddPatientMan;
    @BindView(R.id.ll_add_patient_birthday)
    LinearLayout llAddPatientBirthday;
    @BindView(R.id.rb_add_patient_woman)
    RadioButton rbAddPatientWoman;
    @BindView(R.id.rg_add_patient_sex)
    RadioGroup rgAddPatientSex;


    /**
     * 年月日
     */
    private int year;
    private int month;
    private int day;
    private String date = "";
    private int gender = 1;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, AddPatientActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected AddPatientPresenter createPresenter() {
        return new AddPatientPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_add_patient;
    }

    @Override
    protected void initView() {
        etAddPatientName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                Log.e("TAG", "afterTextChanged: " + editable.toString());
                if (!TextUtils.isEmpty(editable.toString())) {
                    etAddPatientNamePy.setText(Cn2Py.getIndexPinYin(editable.toString()).replace(",", "").toUpperCase());
                } else {
                    etAddPatientNamePy.setText("");
                }

            }
        });

        rgAddPatientSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_add_patient_man:
                        gender = 1;
                        break;
                    case R.id.rb_add_patient_woman:
                        gender = 2;
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {
        //获得今日时间
        Calendar localCalendar = Calendar.getInstance();
        year = localCalendar.get(Calendar.YEAR);
        month = localCalendar.get(Calendar.MONTH);
        day = localCalendar.get(Calendar.DAY_OF_MONTH);
        date = (localCalendar.get(Calendar.YEAR) + "-" + (1 + localCalendar.get(Calendar.MONTH)) + "-" + localCalendar.get(Calendar.DAY_OF_MONTH));
    }

    @Override
    public void addPatientSuccess() {
        ToastUtil.showToast("添加成功");
        finish();
    }

    @OnClick(R.id.btn_add_patient)
    public void addPatient(View v) {
        Patient patient = new Patient();
        patient.phone = etAddPatientPhone.getText().toString();
        patient.name = etAddPatientName.getText().toString();
        patient.birthday = tvAddPatientBirthday.getText().toString();
        patient.gender = gender;
        patient.address = etAddPatientAddress.getText().toString();
        if(TextUtils.isEmpty(patient.name)){
            ToastUtil.showToast("请输入患者姓名");
            return;
        }
        if(TextUtils.isEmpty(patient.birthday)){
            ToastUtil.showToast("请选择出生日期");
            return;
        }
        if(TextUtils.isEmpty(patient.birthday)){
            ToastUtil.showToast("请输入电话");
            return;
        }
        presenter.addPatient(patient);
    }

    @OnClick(R.id.iv_add_patient_back)
    public void exit(View view) {
        finish();
    }

    @OnClick(R.id.ll_add_patient_birthday)
    protected void showDatePickDlg() {
        Calendar.getInstance();
        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                // Log.d(TAG,"year:"+year+"  month:"+month+"   day:"+dayOfMonth);
                AddPatientActivity.this.year = year;
                AddPatientActivity.this.month = month;
                AddPatientActivity.this.day = dayOfMonth;
                date = year + "-" + (month + 1) + "-" + dayOfMonth;
                tvAddPatientBirthday.setText(date);
                etAddPatientAge.setText(TimeUtils.getAgeDetail(date));
            }
        }, year, month, day).show();
    }


}
